package com.dferreira.repository;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.dferreira.repository.data_models.Dependency;
import com.dferreira.repository.data_models.FileEntry;
import com.dferreira.repository.data_models.PackageMetadata;
import com.dferreira.repository.data_models.RepositoryDependency;
import com.dferreira.repository.data_models.RepositoryPackage;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * Created by dferreira on 6/14/14.
 * for LanguagesTeach
 */
public class RepositoryDatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static String TAG = "RepositoryDatabase";
    private static String ON_CREATE = "onCreate";
    private static String ON_DESTROY = "onDestroy";
    private static String ON_UPDATE = "onUpgrade";

    private static final String DATABASE_NAME = "languagesTeach";
    private static final int DATABASE_VERSION = 1;

    private RuntimeExceptionDao<PackageMetadata, Integer> packageMetadataDao = null;
    private RuntimeExceptionDao<FileEntry, Integer> fileEntryDao = null;
    private RuntimeExceptionDao<Dependency, Integer> dependencyDao = null;
    private RuntimeExceptionDao<com.dferreira.repository.data_models.Repository, Integer> repositoryDao = null;
    private RuntimeExceptionDao<RepositoryDependency, Integer> repositoryDependencyDao = null;
    private RuntimeExceptionDao<RepositoryPackage, Integer> repositoryPackageDao = null;

    /**
     * @param context
     */
    public RepositoryDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * @return
     */
    public RuntimeExceptionDao<PackageMetadata, Integer> getPackageMetadataDao() {
        if (packageMetadataDao == null) {
            packageMetadataDao = getRuntimeExceptionDao(PackageMetadata.class);
        }
        return packageMetadataDao;
    }

    /**
     * @return
     */
    public RuntimeExceptionDao<FileEntry, Integer> getFileEntryDao() {
        if (fileEntryDao == null) {
            fileEntryDao = getRuntimeExceptionDao(FileEntry.class);
        }
        return fileEntryDao;
    }


    /**
     * @return
     * @throws SQLException
     */
    public RuntimeExceptionDao<Dependency, Integer> getDependencyDao() {
        if (dependencyDao == null) {
            dependencyDao = getRuntimeExceptionDao(Dependency.class);
        }
        return dependencyDao;
    }

    /**
     * @return
     * @throws SQLException
     */
    public RuntimeExceptionDao<com.dferreira.repository.data_models.Repository, Integer> getRepositoryDao() {
        if (repositoryDao == null) {
            repositoryDao = getRuntimeExceptionDao(com.dferreira.repository.data_models.Repository.class);
        }
        return repositoryDao;
    }


    /**
     * @return
     * @throws SQLException
     */
    public RuntimeExceptionDao<RepositoryDependency, Integer> getRepositoryDependencyDao() {
        if (repositoryDependencyDao == null) {
            repositoryDependencyDao = getRuntimeExceptionDao(RepositoryDependency.class);
        }
        return repositoryDependencyDao;
    }

    /**
     * @return
     * @throws SQLException
     */
    public RuntimeExceptionDao<RepositoryPackage, Integer> getRepositoryPackageDao() {
        if (repositoryPackageDao == null) {
            repositoryPackageDao = getRuntimeExceptionDao(RepositoryPackage.class);
        }
        return repositoryPackageDao;
    }

    /**
     *
     */
    @Override
    public void close() {
        super.close();
        packageMetadataDao = null;
        fileEntryDao = null;
        dependencyDao = null;
        repositoryDao = null;
        repositoryDependencyDao = null;
        repositoryPackageDao = null;
    }

    /**
     * @param db
     * @param connectionSource
     */
    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            Log.i(TAG, ON_CREATE);
            TableUtils.createTable(connectionSource, PackageMetadata.class);
            TableUtils.createTable(connectionSource, FileEntry.class);
            TableUtils.createTable(connectionSource, Dependency.class);
            TableUtils.createTable(connectionSource, com.dferreira.repository.data_models.Repository.class);
            TableUtils.createTable(connectionSource, RepositoryDependency.class);
            TableUtils.createTable(connectionSource, RepositoryPackage.class);
        } catch (SQLException e) {
            Log.e(TAG, ON_CREATE, e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @param db
     * @param connectionSource
     */
    private void onDestroy(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            Log.i(TAG, ON_DESTROY);
            TableUtils.dropTable(connectionSource, FileEntry.class, true);
            TableUtils.dropTable(connectionSource, RepositoryPackage.class, true);
            TableUtils.dropTable(connectionSource, RepositoryDependency.class, true);
            TableUtils.dropTable(connectionSource, PackageMetadata.class, true);
            TableUtils.dropTable(connectionSource, com.dferreira.repository.data_models.Repository.class, true);
        } catch (SQLException e) {
            Log.e(TAG, ON_DESTROY, e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @param db
     * @param connectionSource
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        Log.i(TAG, ON_UPDATE);
        onDestroy(db, connectionSource);
        onCreate(db, connectionSource);
    }

}
